package com.whccb.apps.office.document.button;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ValueListener;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.util.ConsolePrinter;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.whccb.apps.office.document.util.ODFileNumUtil;

/**
 * 
 * 公文发文流程申请文号扩展按钮。
 * 
 * @author xqli
 * 
 */
public class ApplyFileNumButton extends ValueListener {

    @Override
    public String execute(ProcessExecutionContext ctx) throws Exception {
        // String boName = "";// 通过流程参数得到bo名称
        // Map<String, Object> paramMap = SDK.getProcessAPI()
        // .getVariables(ctx.getProcessInstance().getId());
        String boName = SDK.getProcessAPI()
                .getVariable(ctx.getProcessInstance(), "BONAME").toString();
        // if (paramMap != null) {
        // boName = paramMap.get("BONAME").toString();
        // }
        ResponseObject ro = null;
        if (StringUtils.isNotEmpty(boName)) {
            ConsolePrinter.info("boname----" + boName);
            BO draftBo = SDK.getBOAPI().query(boName, true)
                    .detailByBindId(ctx.getProcessInstance().getId());
            ConsolePrinter.info("boname----" + boName);
            if (draftBo != null) {
                String recordTypeId = draftBo.getString("RECORDTYPEID");
                String year = draftBo.getString("YEAR");

                ODFileNumUtil fileNumUtil = new ODFileNumUtil();
                int fileNum = fileNumUtil.getOfficeDocumentFileNum("0",
                        recordTypeId, year);
                BO recod = SDK.getBOAPI().query("BO_WHBK_OD_RECORDTYPE")
                        .detailById(recordTypeId);
                if (fileNumUtil.saveOfficeDocumentFileNum("0", recordTypeId,
                        year, fileNum, ctx.getProcessInstance().getId(), boName,
                        ctx.getUserContext().getUID())) {
                    String odFileNum = "";
                    if ("BO_WHBK_EA_INFO_SEND".equals(boName)) {// 信息上报
                        odFileNum = "第" + fileNum + "期";
                    } else if ("0".equals(recod.getString("RECORDSHOW"))) {
                        odFileNum = fileNum + "";
                    } else {
                        odFileNum = draftBo.getString("RECORDTYPE") + "〔"
                                + draftBo.getString("YEAR") + "〕" + fileNum
                                + " 号 ";
                    }

                    String updateSql = " UPDATE " + boName
                            + " SET NUM = :NUM,FILENUM=:FILENUM WHERE ID = :ID ";
                    Map<String, Object> updateMap = new HashMap<String, Object>();
                    updateMap.put("NUM", fileNum);
                    updateMap.put("FILENUM", odFileNum);
                    updateMap.put("ID", draftBo.getId());
                    DBSql.update(updateSql, updateMap);
                    ro = ResponseObject.newOkResponse();
                    ro.put("fileNum", fileNum);
                    ro.put("odFileNum", odFileNum);
                }
            } else {
                ro = ResponseObject.newErrResponse("参数有误，BO未找到");
            }
        }

        return ro.toString();
    }

}
